<?php
namespace api\modules\v1\controllers;
use common\models\Balance;
use common\models\ExchangeLog;
use common\service\ExchangeService;

/*
	账本兑换 / 划转
*/

class ExchangeController extends CoreController
{
	
	/*
		* 兑换配置信息
		* b_type 账本类型
	*/
	public function actionExchangeConf()
	{
		if(!$btype = $this->request('b_type')) $this->error('账本类型有误');
		$params = ['b_type' => $btype, 'member_id' => $this->_memberId];
		$res = ExchangeService::conf($params);

		if(!$res['state']) $this->error($res['msg']);
		$this->out('兑换配置信息', $res['data']);
	}



	/*
		兑换
		* from_type
		* to_type
		* money 
		* password 密码
	*/
	public function actionIndexInfo()
	{
		if(md5($this->request('password')) != $this->_member['paypwd']) $this->error('密码错误');
		if(!$this->_member['state']) return ['state' => false, 'msg' => '你暂不能此操作'];

		$params = $this->request;
		$params['member_id'] = $this->_memberId;

		$res = ExchangeService::exchange($params, $this->_member);
		if($res['state']) $this->out('操作成功');
		$this->error($res['msg']);
	}

	//兑换需要审核
    public function actionIndexInfo1()
    {
       if(md5($this->request('password')) != $this->_member['paypwd']) $this->error('密码错误');
       if(!$this->_member['state']) return ['state' => false, 'msg' => '你暂不能此操作'];

        $params = $this->request;
        $params['member_id'] = $this->_memberId;
        $state = Setting::getOne(['type' => 'exchange_switch', 'code' => 'exchange_switch'])['value'];
        if($state==1){
            $res = ExchangeService::exchange1($params, $this->_member);
        }else{
            $res = ExchangeService::exchange($params, $this->_member);
        }

        if($res['state']) $this->out('操作成功');
        $this->error($res['msg']);
    }


	/*
		兑换记录
		* page 
		* page_size
		* b_type 选填 默认全部
	*/
	public function actionList()
	{
		$whereArr = array();
		$whereArr['where'] = ['member_id' => $this->_memberId];
		if($this->request('b_type')) $whereArr['where']['from_type'] = $this->request('b_type');
		$params = array(
			'field' => ['*'],
			'page'	=> $this->request('page',1),
			'limit' => $this->request('page_size',15),
			'order' => 'id DESC'
		);		
		$data = ExchangeLog::dataList($whereArr, $params);
		$typeNames = Balance::balanceTypeName();
		foreach($data as &$val)
		{
			$val['describe'] = $typeNames[$val['from_type']] .' to '. $typeNames[$val['to_type']];
		}

		$this->out('兑换列表', $data);
	}


}